//
//  BudgetNetApi.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 16.04.25.
//

import Foundation
import Alamofire

class BudgetNetApi : KoiNetBase {
    static let standard = BudgetNetApi()
    
    func getBudgetTotal(bookId: String)  -> Result<AccountingBudget?, KoiNetError> {
        let result: Result<AccountingBudget?, KoiNetError> = requestAuth(
            path: "budget/total/get",
            method: .get,
            parameters: ["book_id": bookId]
        )
        
        return result
    }
    
    func getBudgetCategory(bookId: String) -> Result<[AccountingBudget]?, KoiNetError> {
        // 同步请求
        let result: Result<[AccountingBudget]?, KoiNetError> = requestAuth(
            path: "budget/category/list",
            method: .get,
            parameters: ["book_id": bookId]
        )
        
        return result
    }
    
    func budgetCreate(budgetJson: String, budgetTotalJson: String? = nil) -> Result<KoiDataCommonResponse?, KoiNetError> {
        var params = ["budget": budgetJson]
        if budgetTotalJson != nil {
            params["budget_total"] = budgetTotalJson
        }
        let result: Result<KoiDataCommonResponse?, KoiNetError> = requestAuth(
            path: "budget/record/create",
            method: .put,
            parameters: params
        )
        return result
    }
    
    func budgetUpdate(bookId: String, budgetId: String, budgetJson: String, budgetTotalJson: String? = nil) -> Result<KoiDataCommonResponse?, KoiNetError> {
        var params = ["budget": budgetJson]
        if budgetTotalJson != nil {
            params["budget_total"] = budgetTotalJson
        }
        let result: Result<KoiDataCommonResponse?, KoiNetError> = requestAuth(
            path: "budget/record/\(bookId)/\(budgetId)",
            method: .post,
            parameters: params
        )
        return result
    }
    
    func budgetDelete(bookId: String, budgetId: String) -> Result<KoiDataCommonResponse?, KoiNetError> {
        let result: Result<KoiDataCommonResponse?, KoiNetError> = requestAuth(
            path: "budget/record/\(bookId)/\(budgetId)",
            method: .delete
        )
        return result
    }
    
    func budgetDeleteAll(bookId: String) -> Result<KoiDataCommonResponse?, KoiNetError> {
        let result: Result<KoiDataCommonResponse?, KoiNetError> = requestAuth(
            path: "budget/record/clear/\(bookId)",
            method: .delete
        )
        return result
    }
}
